package com.test.fundible.fundible_05_process;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentActivity;

import com.test.fundible.R;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_01_launcher.App_fundibleShow_fundibleCustomize;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class Delete_fundibleAccountDi_fundiblealog_Fr extends Dialog {

    private final FragmentActivity activity_fundible;

    private final Ba_fundiblenkAcc_fundibleount_Fr account_fundible;

    private final String page_fundible;

    private final Runnable dltActRespRunnable_fundible;

    public Delete_fundibleAccountDi_fundiblealog_Fr(@NonNull FragmentActivity activity,
                                                    Ba_fundiblenkAcc_fundibleount_Fr account,
                                                    String page,
                                                    Runnable dltActRespRunnable) {
        super(activity);
        this.activity_fundible = activity;
        this.account_fundible = account;
        this.page_fundible = page;
        this.dltActRespRunnable_fundible = dltActRespRunnable;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fundible_delete_account_fr);

        getWindow().setBackgroundDrawableResource(R.color.transparent);
        setCanceledOnTouchOutside(false);
        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);


        initView_fundible();
    }

    @SuppressLint("SetTextI18n")
    private void initView_fundible(){

        TextView accountmobileTv  = findViewById(R.id.dfdaf_tv_mobile);
        ImageView accountlogoIv = findViewById(R.id.dfdaf_iv_logo);
        TextView accountchannelTv = findViewById(R.id.dfdaf_tv_channel);

        //取消
        findViewById(R.id.dfdaf_btn_cancel).setOnClickListener(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"_DELETE_CANCEL_CLICK", account_fundible.getWalletMobile_fundible());
            dismiss();
        });

        //提交
        findViewById(R.id.dfdaf_btn_submit).setOnClickListener(v -> {
            FundibleUtil.startLoading(getContext(),"");
            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"_DELETE_CONFIRM_CLICK", account_fundible.getWalletMobile_fundible());
            OkHttpUtil.getInstance_fundible().deleteWalletAccount_Fr(account_fundible.getId_fundible(),() -> {
                if (dltActRespRunnable_fundible !=null) dltActRespRunnable_fundible.run();
            });
            dismiss();
        });

        if (account_fundible != null){

            String channelDesc = account_fundible.getWalletDesc_fundible();

            accountmobileTv.setText(account_fundible.getWalletMobile_fundible());
            accountchannelTv.setText(channelDesc);

            //加载图标
            if (App_fundibleShow_fundibleCustomize.getInstance_fundible().getChannelLogoByDesc_fundible(channelDesc) == null){
                new Thread(() -> {
                    try {
                        if (TextUtils.isEmpty(account_fundible.getWalletLogo_fundible())) return;
                        if (!App_fundibleShow_fundibleCustomize.getInstance_fundible().hasAccountChannel_fundible()) return;
                        URL url = new URL(account_fundible.getWalletLogo_fundible());
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        connection.setConnectTimeout(5000);
                        connection.setRequestMethod("GET");
                        if (connection.getResponseCode()==200){
                            InputStream inputStream = connection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                            App_fundibleShow_fundibleCustomize.getInstance_fundible().setChannelLogoByDesc_fundible(channelDesc,bitmap);
                            FundibleUtil.safeRun(() -> accountlogoIv.setImageBitmap(bitmap));
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();
            }else accountlogoIv.setImageBitmap(App_fundibleShow_fundibleCustomize.getInstance_fundible().getChannelLogoByDesc_fundible(channelDesc));

        }

    }


}
